/* 
 * cms，一个基于J2EE架构内容管理系统
 * Copyright © GrayRabbit Co., Ltd. All rights reserved.
 * Department:运营department
 * 更多信息请访问：
 * http://code.google.com/p/gray-rabbit-cms/
 */
package org.grayrabbit.cms.action.search;

import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.grayrabbit.cms.action.common.BaseAction;
import org.grayrabbit.cms.entity.HotKeywordRule;
import org.grayrabbit.cms.service.search.HotKeywordRuleService;
import org.grayrabbit.cms.service.search.impl.HotKeywordServiceImpl;
import org.grayrabbit.cms.vo.search.HotKeywordRuleBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import org.grayrabbit.dao.util.Page;

/**
 * <b>HotKeywordRuleAction</b> Struts2 action�?
 * 
 * @version 1.0,创建时间:2011-05-16 14:51:23
 */
@Controller
@Scope("prototype")
@Results({
		@Result(name = "reload", type = "redirectAction", location = "hotkeywordrule-list"),
		@Result(name = "success", type = "dispatcher", location = "hotkeywordrule-list.jsp"),
		@Result(name = "input", type = "dispatcher", location = "hotkeywordrule-input.jsp")
		})
public class HotKeywordRuleAction extends BaseAction<HotKeywordRuleBean> {
	private static final long serialVersionUID = -1L;
	// Service接口接入
	@Autowired
	@Qualifier("hotKeywordRuleServiceImpl")
	private HotKeywordRuleService hotKeywordRuleService;

	// VO对象，由前端页面数据组装
	private HotKeywordRuleBean hotKeywordRule;

	/** return 前端页面数据组装VO对象,<b>HotKeywordRuleBean</b> */
	public HotKeywordRuleBean getHotKeywordRule() {
		return hotKeywordRule;
	}

	public void setHotKeywordRule(HotKeywordRuleBean hotKeywordRule) {
		this.hotKeywordRule = hotKeywordRule;
	}

	// 分页对象
	private Page<HotKeywordRule> page = new Page<HotKeywordRule>(MAX_PAGE_NUM,
			true);

	/*
	 * return分页对象，默认实例化。如：page.pageNu=2,将更新相应的对象property
	 */
	public Page<HotKeywordRule> getPage() {
		return page;
	}

	// 接收前端form,url提交参数组装成分页property
	public void setPage(Page<HotKeywordRule> page) {
		this.page = page;
	}

	/**
	 * 在执行（struts2的action调用时）list()方法前执行本方法
	 */
	public void prepareList() {
		prepareModel();
	}

	/**
	 * 在执行任何其它action方法（struts2的action调用时）前执行本方法
	 */
	public void prepare() {
		if (hotKeywordRule == null) {
			hotKeywordRule = new HotKeywordRuleBean();
		}
	}

	/**
	 * 实现ModelDriven的接口简例前端页的bean组装写法, 如：
	 * 
	 * <pre>
	 * 	<code> name="user.userName"</code>
	 * 改写成：
	 * 	<code> name="userName"</code>
	 * action实例中，会自动组成user成员变量
	 * </pre>
	 */
	public HotKeywordRuleBean getModel() {
		return hotKeywordRule;
	}

	@Override
	protected void prepareModel() {
		if (hotKeywordRule != null
				&& StringUtils.isNotBlank(hotKeywordRule.getHotKeywordRuleId())) {
			hotKeywordRule = (HotKeywordRuleBean) dozer.map(
					hotKeywordRuleService.getHotKeywordRule(hotKeywordRule
							.getHotKeywordRuleId()), HotKeywordRuleBean.class);
		}

	}

	/**
	 * 删除<b>HotKeywordRule</b>记录的方法，成员变量<b>key</b>为主键的String数组，以主键方式进行删作
	 * 删除成功后，返回"reload"，然后跳转到hotkeywordrule-list.action
	 */
	@Override
	public String delete() {
		hotKeywordRuleService.delete(key);
		return RELOAD;
	}

	/**
	 * HotKeywordRule的列表方法，通过成员变量<b>page�?{insClassName}</b>调用service接口，更新page
	 * 执行成功后，将映射到hotkeywordrule-list.jsp页面
	 */
	@Override
	@Action(value = "hotkeywordrule-list")
	public String list() {
		hotKeywordRuleService.list(page, hotKeywordRule);
		return SUCCESS;
	}

	/**
	 * HotKeywordRule的新增与编辑的方法执行成功能 将映射到hotkeywordrule-input.jsp
	 */
	public String input() {
		prepareModel();
		return INPUT;
	}
	/**
	 * 提交定义值
	 */
	@Action(value = "hotkeywordrule-submitvalue")
	public String submitValue() {
		String text = "";
		HotKeywordRule obj = null;
		try{
			if (hotKeywordRule != null && StringUtils.isNotBlank(hotKeywordRule.getHotKeywordRuleId())) {
				obj = hotKeywordRuleService.getHotKeywordRule(hotKeywordRule.getHotKeywordRuleId());
				obj.setItemValue(hotKeywordRule.getItemValue());
			} 
			hotKeywordRuleService.save(obj);
			updateRules(obj);
			text = "ok";
		}catch (Exception e) {
			log.debug("异常：{}", e);
			text="no";
		}
		return renderText(text); 
	}
	
	private void updateRules(HotKeywordRule obj){
		Map<String, Long> map = HotKeywordServiceImpl.map;
		String hotType = obj.getItemCode().trim();
		long value = Long.valueOf(obj.getItemValue());
		map.put(hotType, value);
		 
	}
	

	/**
	 * save or update HotKeywordRule的方法，将成员property
	 * hotKeywordRule转成HotKeywordRule实例，执行保存；
	 * 执行成功后，返回"reload"，然后跳转到hotkeywordrule-list.action
	 */
	@Override
	public String save() {
		HotKeywordRule obj = (HotKeywordRule) dozer.map(hotKeywordRule,
				HotKeywordRule.class);
		if (!StringUtils.isNotBlank(obj.getHotKeywordRuleId())) {
			obj.setHotKeywordRuleId(null);
		}
		hotKeywordRuleService.save(obj);
		return RELOAD;
	}
}